package com.zygame.sdk.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zygame.sdk.utils.res.FYResource;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.view.DeleteNameDailog;

/**
 * 
 * @Cindigo
 * @author k
 *
 */


//自定义适配器Adapter
public class OptionsAdapter extends BaseAdapter {

	private ArrayList<Map<String,String>> list = new ArrayList<Map<String,String>>(); 
    private Context activity = null;
	private Handler handler;
    
	/**
	 * 自定义构造方法
	 * @param activity
	 * @param handler
	 * @param list
	 */
    public OptionsAdapter(Context activity, Handler handler, ArrayList<Map<String,String>> list){
    	this.activity = activity;
    	this.handler = handler;
    	this.list = list;
    }
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public Map<String,String> getText(int postion){
		return list == null? new HashMap<String, String>():list.get(postion);
		
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null; 
        if (convertView == null) { 
            holder = new ViewHolder(); 
            //下拉项布局
            convertView = LayoutInflater.from(activity).inflate(ResourceUtil.getLayoutId(activity, FYResource.layout.fy_ly_item_options), null);
            holder.textView = (TextView) convertView.findViewById(ResourceUtil.getId(activity, "item_text"));   
            holder.imageView = (ImageView) convertView.findViewById(ResourceUtil.getId(activity, "delImage")); 
            
            convertView.setTag(holder); 
        } else { 
            holder = (ViewHolder) convertView.getTag(); 
        } 
        
        holder.textView.setText(list.get(position).entrySet().iterator().next().getKey());
        
        //为下拉框选项文字部分设置事件，最终效果是点击将其文字填充到文本框
        holder.textView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle data = new Bundle();
				//设置选中索引
				data.putInt("selIndex", position);
				msg.setData(data);
				msg.what = 1;
				//发出消息
				handler.sendMessage(msg);
			}
		});
        
        //为下拉框选项删除图标部分设置事件，最终效果是点击将该选项删除
        holder.imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new DeleteNameDailog(activity,list, handler,position).show();
			}
		});
        
        return convertView; 
	}

}


class ViewHolder { 
    TextView textView; 
    ImageView imageView; 
}
